package com.system.service.imp;

import com.common.Enums.ResponseEnum;
import com.common.exception.CourseException;
import com.common.util.CopyUtil;
import com.common.util.UuidUtil;
import com.core.system.domain.RoleUser;
import com.core.system.dto.RoleUserDto;
import com.core.system.mapper.RoleUserMapper;
import com.core.system.service.imp.RoleUserServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;



@Service
@Primary
public class RoleUserExServiceImp   extends RoleUserServiceImp  {
    @Autowired
    private RoleUserMapper roleUserMapper;
    /**
     * 列表查询
     */

    /**
     * 保存，id有值时更新，无值时新增
     */
    @Override
    public ResponseEntity save(RoleUserDto roleUserDto) {
        RoleUser roleUser = CopyUtil.copy(roleUserDto, RoleUser.class);
        if (StringUtils.isEmpty(roleUserDto.getId())) {
           return this.insert(roleUser);
        } else {
            return this.update(roleUser);
        }

    }
    /**
     * 更新
     */
    private ResponseEntity update(RoleUser roleUser) {
        int i = roleUserMapper.updateById(roleUser);

        if(i<=0){
            throw new CourseException(ResponseEnum.UPDATE_ERROR);
        }
        return ResponseEntity.ok().build();
    }
    /**
     * 新增
     */
    private ResponseEntity insert(RoleUser roleUser) {
        roleUser.setId(UuidUtil.getShortUuid());
        int i = roleUserMapper.insert(roleUser);
        if(i<=0){
            throw new CourseException(ResponseEnum.ADD_ERROR);
        }
        return ResponseEntity.ok().build();
    }

}
